use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use crate::models::entity::perm;

/// 权限详情
#[derive(Debug, Deserialize, Serialize, ToSchema)]
pub struct PermDetail {
    pub id: i32,
    pub name: String,
    pub desc: Option<String>,
    pub path: String,
    pub parent_id: i32,
    pub order: i32,
    pub create_time: String,
    pub update_time: String,
}

/// 权限列表
#[derive(Debug, Deserialize, Serialize, ToSchema)]
pub struct PermListInfo {
    pub id: i32,
    pub name: String,
    pub desc: Option<String>,
    pub path: String,
    pub parent_id: i32,
    pub order: i32,
    pub create_time: String,
    pub update_time: String,
}

impl From<perm::Model> for PermDetail {
    fn from(value: perm::Model) -> Self {
        let fmt = "%Y-%m-%d %H:%M:%S";
        Self {
            id: value.id,
            name: value.name,
            desc: value.desc,
            path: value.path,
            parent_id: value.parent_id,
            order: value.order,
            create_time: value.create_at.format(fmt).to_string(),
            update_time: value.update_at.format(fmt).to_string(),
        }
    }
}

impl From<perm::Model> for PermListInfo {
    fn from(value: perm::Model) -> Self {
        let fmt = "%Y-%m-%d %H:%M:%S";
        Self {
            id: value.id,
            name: value.name,
            desc: value.desc,
            path: value.path,
            parent_id: value.parent_id,
            order: value.order,
            create_time: value.create_at.format(fmt).to_string(),
            update_time: value.update_at.format(fmt).to_string(),
        }
    }
}
